﻿using STT.Business;
using System;
using System.Data;
using STT.Entities;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace STT.Admin.UserControls
{
    public partial class ucQuanLyTinNhan : System.Web.UI.UserControl
    {
        public DataTable tbUser;
        public DataTable tb;
        protected void Page_Load(object sender, EventArgs e)
        {
            
            tbUser = BTbDangNhap.SelectAllChatForManager(Session["TenDN"].ToString());

            LoadMessage();
        }        

        void LoadMessage()
        {
            if (Request.QueryString["idmessage"] != null)
            {
                string idmessage = Request.QueryString["idmessage"].ToString().Trim();

                lvTinNhan.DataSource = BTbChat.SelectMessageForManager(Session["TenDN"].ToString(), idmessage,"").Tables[0];

                lvTinNhan.DataBind();
            }
        }

        protected void btSearch_Click(object sender, System.Web.UI.ImageClickEventArgs e)
        {
                tbUser = BTbDangNhap.SelectByName(txtTimKiem.Text);
        }

        protected void lvTinNhan_ItemCommand(object sender, System.Web.UI.WebControls.ListViewCommandEventArgs e)
        {
            if(e.CommandName=="Delete")
            {
                BTbChat.Delete(
                lvTinNhan.DataKeys[e.Item.DataItemIndex]["IdGui"].ToString().TrimEnd(),
                lvTinNhan.DataKeys[e.Item.DataItemIndex]["IdNhan"].ToString().TrimEnd(),
                lvTinNhan.DataKeys[e.Item.DataItemIndex]["ThoiGianGui"].ToString().TrimEnd());                

                LoadMessage();
            }
        }

        void ThongBao(string Loi)
        {
            ScriptManager.RegisterClientScriptBlock(this, GetType(), "Thông báo!", "<script>alert('" + Loi + "')</script>", false);
        }

        protected void lvTinNhan_ItemDeleting(object sender, System.Web.UI.WebControls.ListViewDeleteEventArgs e)
        {

        }

        public string  Display(string MaHV)
        {
            if (MaHV.TrimEnd() == Session["TenDN"].ToString())
                return "delete";      
      
            return "hide";
        }
        public string lastTime="";

        protected void lvTinNhan_ItemDataBound(object sender, System.Web.UI.WebControls.ListViewItemEventArgs e)
        {
            if (e.Item.ItemType == ListViewItemType.DataItem)
            {
                DataRowView rowView = e.Item.DataItem as DataRowView;

                if (lastTime != DateTime.Parse(rowView["ThoiGianGui"].ToString()).ToShortDateString())
                {
                    e.Item.Controls.AddAt(0, new Literal() { Text = "<div class='date_spend'>" + DateTime.Parse(rowView["ThoiGianGui"].ToString()).ToShortDateString() + "</div>" });
                    lastTime = DateTime.Parse(rowView["ThoiGianGui"].ToString()).ToShortDateString();
                }
            }
        }
    }
}